﻿using System;
using System.Collections.Generic;
using X.DataFactory;
using X.Entity;
using System.Data;

namespace X.Data.Access
{
    public class Page : IPage
    {

        public PageInfo GetPageInfo(string pageKey)
        {
            string sSQL = "SELECT * FROM X_Page WHERE PageKey = @PageKey";

            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@PageKey", DbType.String, pageKey);

            PageInfo pageInfo;

            using (NullableDataReader reader = DBHelper.ExecuteReaderSql(sSQL, cmdParams))
            {
                while (reader.Read())
                {
                    return PageFromReader(reader);
                }

                pageInfo = new PageInfo(true);
            }

            return pageInfo;
        }

        public bool Update(PageInfo pageInfo)
        {
            string sSQL = @"UPDATE [X_Page]
                            SET [Title] = @Title
                            ,[Keyword] = @Keyword
                            ,[Description] = @Description
                            ,[PageContent] = @PageContent
                            ,[PageDescription] = @PageDescription
                            WHERE [PageKey] = @PageKey";


            Parameters cmdParams = new Parameters();
            cmdParams.AddInParameter("@Title", DbType.String, pageInfo.Title);
            cmdParams.AddInParameter("@Keyword", DbType.String, pageInfo.Keyword);
            cmdParams.AddInParameter("@Description", DbType.String, pageInfo.Description);
            cmdParams.AddInParameter("@PageContent", DbType.String, pageInfo.PageContent);
            cmdParams.AddInParameter("@PageDescription", DbType.String, pageInfo.PageDescription);
            cmdParams.AddInParameter("@PageKey", DbType.String, pageInfo.PageKey);

            return DBHelper.ExecuteSql(sSQL, cmdParams);
        }

        public IList<PageInfo> GetPageList()
        {
            string sSQL = "SELECT * FROM X_Page";

            IList<PageInfo> list = new List<PageInfo>();

            using (NullableDataReader reader = DBHelper.ExecuteReaderSql(sSQL))
            {
                while (reader.Read())
                {
                    list.Add(PageFromReader(reader));
                }
            }

            return list;
        }

        private PageInfo PageFromReader(NullableDataReader reader)
        {
            PageInfo pageInfo = new PageInfo();

            pageInfo.PageKey = reader.GetString("PageKey");
            pageInfo.Title = reader.GetString("Title");
            pageInfo.Keyword = reader.GetString("Keyword");
            pageInfo.Description = reader.GetString("Description");
            pageInfo.PageContent = reader.GetString("PageContent");
            pageInfo.PageName = reader.GetString("PageName");
            pageInfo.PageDescription = reader.GetString("PageDescription");

            return pageInfo;
        }


        public IList<PageInfo> GetPageList(string[] pageKeys)
        {
            string sSQL = "SELECT * FROM X_Page WHERE PageKey IN(";

            string temp = string.Empty;

            for (int i = 0; i < pageKeys.Length; i++)
            {
                if (string.Empty != temp)
                    temp += ",";
                temp += string.Format("'{0}'", pageKeys[i]);
            }

            sSQL += temp + ")";

            IList<PageInfo> list = new List<PageInfo>();

            using (NullableDataReader reader = DBHelper.ExecuteReaderSql(sSQL))
            {
                while (reader.Read())
                {
                    list.Add(PageFromReader(reader));
                }
            }

            return list;
        }
    }
}
